package com.qf.business.user.controller;

import com.alibaba.fastjson.JSON;
import com.qf.business.user.service.TeacherService;
import com.qf.commons.base.result.R;
import com.qf.commons.base.result.Rs;
import com.qf.commons.core.utils.JwtUtils;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.aspect.utils.UserUtils;
import com.qf.data.user.entity.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * author Ken
 * create_time 2022/11/25
 */
@RestController
@RequestMapping("/user/teacher")
@Slf4j
public class TeacherControler {

    @Autowired
    private TeacherService teacherService;

    /**
     * 通过邮件查询教师信息
     * @return
     */
    @RequestMapping("/queryByEmail")
    public R<Teacher> queryByEmail(String email){
        Teacher teacher = teacherService.queryByEmail(email);
        return Rs.create(teacher);
    }

    /**
     * 修改教师的信息
     * @return
     */
    @GetUser
    @RequestMapping("/updateTeacherInfo")
    public R updateTeacherInfo(Teacher updateTeacher){
        //获取登录的教师信息
        log.debug("[update teacher] 修改教师信息！");

        //登录的教师id
        Teacher teacher = UserUtils.getUser(Teacher.class);

        //判断 登录的教师id 必须和 修改的教师id是同一个，否则说明是恶意修改信息
        if (teacher.getId() == updateTeacher.getId()) {
            this.teacherService.updateById(updateTeacher);
        }

        String jwtToken = JwtUtils.createJwtToken("user", JSON.toJSONString(updateTeacher));
        return Rs.create(jwtToken);
    }
}
